﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Script.Serialization;
using NFine.Application;
using NFine.Application.InformationCenter;
using NFine.Code;
using NFine.Domain._03_Entity.InformationCenter;
using WeiXin.SDK;
using NFine.Web.App_Start;

namespace NFine.Web.Areas.InformationCenter.Controllers
{
    [HandlerLogin]
    public class NewsController : ControllerBase
    {
        private SysNewsApp newsApp = new SysNewsApp();
        private string moduleName = "信息模块";

        [HttpGet]
        [HandlerAjaxOnly]
        public ActionResult GetGridJson(Pagination pagination, string keyword)
        {
            var data = new
            {
                rows = newsApp.GetList(pagination, keyword),
                total = pagination.total,
                page = pagination.page,
                records = pagination.records
            };
            return Content(data.ToJson());
        }
        [HttpGet]
        [HandlerAjaxOnly]
        public ActionResult GetGridJsonWX(Pagination pagination, string keyword)
        {
            var d = MaterialAPI.GetMediaList(WeixinConfig.TokenHelper.GetToken(), (pagination.page-1)*pagination.rows);
            List<SysNewsEntity> lstopview = new List<SysNewsEntity>();
            if (d.item_count > 0)
            {
                var items = d.item;
                for (int i = 0; i < d.item_count; i++)
                {
                    var item = items[i];
                    SysNewsEntity sysNew = new SysNewsEntity();
                    sysNew.F_Title = item.content.news_item[0].title;
                    sysNew.F_Id = item.content.news_item[0].url;
                    sysNew.F_Content = item.content.news_item[0].digest;
                    //sysNew.IMGsrc = "upload/news/" + name + ".jpg";
                    lstopview.Add(sysNew);
                }
            }
            d = MaterialAPI.GetMediaCount(WeixinConfig.TokenHelper.GetToken());
            var data = new
            {
                rows = lstopview,
                total = d.news_count,
                page = pagination.page,
                records = pagination.records
            };
            return Content(data.ToJson());
        }
        [HttpGet]
        [HandlerAjaxOnly]
        public ActionResult GetFormJson(string keyValue)
        {
            //获取商品信息
            var data = newsApp.GetForm(keyValue);
            return Content(data.ToJson());
        }

        [HttpPost]
        [HandlerAjaxOnly]
        [ValidateInput(false)]
        public ActionResult SubmitForm(SysNewsEntity entity, string keyValue)
        {
            try
            {
                var curUser = OperatorProvider.Provider.GetCurrent();
                if (string.IsNullOrWhiteSpace(keyValue))
                {
                    entity.F_CreatorUserId = curUser.UserId;
                    entity.F_CreatorTime = DateTime.Now;
                }
                else
                {
                    entity.F_LastModifyUserId = curUser.UserId;
                    entity.F_LastModifyTime = DateTime.Now;
                }
                newsApp.SubmitForm(entity, keyValue);
                return Success("操作成功。");
            }
            catch (Exception ex)
            {
                addLog(moduleName, DbLogType.Create.ToString(), false, "保存信息失败，" + ex.Message);
                return Error("操作失败，服务器端发生错误。");
            }
        }

        [HttpPost]
        [HandlerAjaxOnly]
        [ValidateAntiForgeryToken]
        public ActionResult DeleteForm(string keyValues)
        {
            try
            {
                int result = newsApp.DeleteNews(keyValues);
                if (result > 0)
                {
                    addLog(moduleName, DbLogType.Delete.ToString(), true, "删除信息成功");
                    return Success("删除成功。");
                }
                else
                {
                    addLog(moduleName, DbLogType.Delete.ToString(), false, "删除信息失败");
                    return Error("删除失败。");
                }
            }
            catch (Exception ex)
            {
                addLog(moduleName, DbLogType.Delete.ToString(), false, "删除信息失败，" + ex.Message);
                return Error("删除失败：" + ex);
            }
        }


        
        public override ActionResult Details()
        {
            var model = newsApp.GetForm(Request["keyValue"]);
            return View(model);

        }
    }
}